
<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>异常记录 - 智能水表抄表系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/static/styles/main.css">
</head>

<body>
    <header class="header">
        <h1><i class="fas fa-tint"></i> 智能水表抄表系统</h1>
    </header>

    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm">
        <div class="container">
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/"><i class="fas fa-home"></i> 首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/history"><i class="fas fa-history"></i> 历史记录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/bills"><i class="fas fa-file-invoice-dollar"></i> 账单管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/anomalies"><i class="fas fa-exclamation-triangle"></i> 异常记录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/water_plans"><i class="fas fa-project-diagram"></i> 水务规划</a>
                    </li>
                    {% if user and user.role == "admin" %}
                    <li class="nav-item">
                        <a class="nav-link" href="/admin"><i class="fas fa-user-shield"></i> 管理控制台</a>
                    </li>
                    {% endif %}
                </ul>
                <div class="navbar-nav">
                    <span class="nav-item nav-link">
                        <i class="fas fa-user"></i> {{ user.username }}
                    </span>
                    <a class="nav-link" href="/logout"><i class="fas fa-sign-out-alt"></i> 退出登录</a>
                </div>
            </div>
        </div>
    </nav>

    <div class="container py-4">
        {% if error %}
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ error }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endif %}

        {% if success %}
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ success }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endif %}

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="d-flex justify-content-between align-items-center">
                    <h2><i class="fas fa-exclamation-triangle"></i> 异常记录</h2>
                    <a href="/report_anomaly" class="btn btn-primary">
                        <i class="fas fa-plus"></i> 上报异常
                    </a>
                </div>
            </div>
        </div>

        <!-- 异常记录统计卡片 -->
        <div class="row mb-4">
            <div class="col-md-4 mb-4">
                <div class="card bg-danger text-white">
                    <div class="card-body text-center">
                        <i class="fas fa-exclamation-circle fa-3x mb-3"></i>
                        <h3>{{ anomalies|length }}</h3>
                        <p>总异常数</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mb-4">
                <div class="card bg-warning text-white">
                    <div class="card-body text-center">
                        <i class="fas fa-clock fa-3x mb-3"></i>
                        <h3>{{ anomalies|selectattr('status', 'equalto', 'unresolved')|list|length }}</h3>
                        <p>待处理异常</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4 mb-4">
                <div class="card bg-success text-white">
                    <div class="card-body text-center">
                        <i class="fas fa-check-circle fa-3x mb-3"></i>
                        <h3>{{ anomalies|selectattr('status', 'equalto', 'resolved')|list|length }}</h3>
                        <p>已解决异常</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="card shadow-sm mb-4">
            <div class="card-header bg-light">
                <h3 class="card-title mb-0"><i class="fas fa-list"></i> 异常记录列表</h3>
            </div>
            <div class="card-body">
                {% if anomalies %}
                <div class="table-container">
                    <table class="custom-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>水表编码</th>
                                <th>异常类型</th>
                                <th>描述</th>
                                <th>状态</th>
                                <th>报告日期</th>
                                <th>解决日期</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for anomaly in anomalies %}
                            <tr>
                                <td>{{ anomaly.id }}</td>
                                <td>{{ anomaly.meter_code }}</td>
                                <td>
                                    {% if anomaly.anomaly_type == 'reading_decreased' %}
                                    <span class="badge bg-danger">读数减少</span>
                                    {% elif anomaly.anomaly_type == 'excessive_usage' %}
                                    <span class="badge bg-warning">用量异常</span>
                                    {% elif anomaly.anomaly_type == 'invalid_reading' %}
                                    <span class="badge bg-info">无效读数</span>
                                    {% else %}
                                    <span class="badge bg-secondary">其他异常</span>
                                    {% endif %}
                                </td>
                                <td>{{ anomaly.description }}</td>
                                <td>
                                    {% if anomaly.status == 'resolved' %}
                                    <span class="badge bg-success">已解决</span>
                                    {% else %}
                                    <span class="badge bg-warning">待处理</span>
                                    {% endif %}
                                </td>
                                <td>{{ anomaly.report_date }}</td>
                                <td>{{ anomaly.resolved_date if anomaly.resolved_date else '-' }}</td>
                                <td class="actions">
                                    <a href="/anomaly/{{ anomaly.id }}" class="btn btn-sm btn-info">
                                        <i class="fas fa-eye"></i> 详情
                                    </a>
                                    {# Keep the button here, but move the modal definition outside the loop #}
                                    {% if anomaly.status == 'unresolved' and user.role == 'admin' %}
                                    <button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal"
                                            data-bs-target="#resolveModal{{ anomaly.id }}">
                                        <i class="fas fa-check"></i> 解决
                                    </button>
                                    {% endif %}
                                </td>
                            </tr>
                            {# REMOVE the modal definition from HERE #}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <div class="alert alert-info">
                    <i class="fas fa-info-circle"></i> 没有发现异常记录，水表运行正常！
                </div>
                {% endif %}
            </div>
        </div>
    </div> {# End of container #}

    {# --- Move Modal Definitions Outside the Container/Table Loop --- #}
    {% for anomaly in anomalies %}
        {% if anomaly.status == 'unresolved' and user.role == 'admin' %}
        <div class="modal fade" id="resolveModal{{ anomaly.id }}" tabindex="-1"
            aria-labelledby="resolveModalLabel{{ anomaly.id }}" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    {# The form action is correct, it will be submitted when the button inside the modal is clicked #}
                    <form action="/resolve_anomaly/{{ anomaly.id }}" method="POST">
                        <div class="modal-header">
                            <h5 class="modal-title" id="resolveModalLabel{{ anomaly.id }}">解决异常 #{{ anomaly.id }}</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="mb-3">
                                <label for="resolution{{ anomaly.id }}" class="form-label">解决方案</label>
                                <textarea class="form-control" id="resolution{{ anomaly.id }}" name="resolution"
                                    rows="4" required></textarea>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary"
                                data-bs-dismiss="modal">取消</button>
                            <button type="submit" class="btn btn-success">确认解决</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        {% endif %}
    {% endfor %}
    {# --- End of Modal Definitions --- #}

    <footer class="footer">
        <div class="container">
            <p>&copy; 2023 智能水表抄表系统 | 提供水表读数和编码识别</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
    {# Optional: Add any custom JavaScript here if needed #}
    <script>
        // Example: You can add listeners here if you need to do something *before* the modal shows
        // or after it hides, but the data-bs- attributes usually handle the core showing/hiding.
        // If you needed to pre-fill the modal or do something dynamic when it opens:
        /*
        var resolveModals = document.querySelectorAll('.modal');
        resolveModals.forEach(function (modalElement) {
          modalElement.addEventListener('show.bs.modal', function (event) {
            // Logic to run when a resolve modal is about to be shown
            // You might extract the anomaly ID from the relatedTarget button
            var button = event.relatedTarget; // Button that triggered the modal
            var anomalyId = button.getAttribute('data-bs-target').replace('#resolveModal', '');
            console.log('Modal for Anomaly ID:', anomalyId, 'is about to be shown');
            // Find elements inside the modal if needed
            var modalTitle = modalElement.querySelector('.modal-title');
            modalTitle.textContent = '解决异常 #' + anomalyId; // Example: Update title
          });
        });
        */
        // The console.log that was on the button itself is removed in the fix,
        // as it's not necessary for modal functionality and can be distracting.
        // If you still want to log the button click:
        /*
        document.querySelectorAll('.actions button[data-bs-toggle="modal"]').forEach(button => {
            button.addEventListener('click', () => {
                const targetId = button.getAttribute('data-bs-target');
                console.log('Resolve button clicked for target:', targetId);
            });
        });
        */
    </script>

</body>

</html>